<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_vpage_template.class.php');


class TCMS_VPage_Template_DBC extends TCMS_VPage_Template implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select name, wp_path, wp_class_name, page_name, cache, cache_expiry, hard_cache, edit_path, man_path, del_path from tcms_vpage_templates where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_name(stripslashes($rez_obj->NAME));
			$this->set_wp_path(stripslashes($rez_obj->WP_PATH));
			$this->set_wp_class_name(stripslashes($rez_obj->WP_CLASS_NAME));
			$this->set_page_name(stripslashes($rez_obj->PAGE_NAME));
			$this->set_cache($rez_obj->CACHE);
			$this->set_cache_expiry($rez_obj->CACHE_EXPIRY);
			$this->set_hard_cache($rez_obj->HARD_CACHE);
			$this->set_edit_path(stripslashes($rez_obj->EDIT_PATH));
			$this->set_man_path(stripslashes($rez_obj->MAN_PATH));
			$this->set_del_path(stripslashes($rez_obj->DEL_PATH));

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_vpage_templates_seq');
		$name = addslashes($this->get_name());
		$wp_path = addslashes($this->get_wp_path());
		$wp_class_name = addslashes($this->get_wp_class_name());
		$page_name = addslashes($this->get_page_name());
		$cache = $this->get_cache() ? 1 : 0;
		$cache_expiry = $this->get_cache_expiry();
		$hard_cache = $this->get_hard_cache() ? 1 : 0;
		$edit_path = addslashes($this->get_edit_path());
		$man_path = addslashes($this->get_man_path());
		$del_path = addslashes($this->get_del_path());

		if ($id) {
			$sql = "insert into tcms_vpage_templates (id, ".
									      "name, ".
									      "wp_path, ".
									      "wp_class_name, ".
									      "page_name, ".
									      "cache, ".
									      "cache_expiry, ".
									      "hard_cache, ".
									      "edit_path, ".
									      "man_path, ".
									      "del_path ".
									      ") ".
								"values ".
									      "($id,".
									      "'$name', ".
									      "'$wp_path', ".
									      "'$wp_class_name', ".
									      "'$page_name', ".
									      "$cache, ".
									      "$cache_expiry, ".
									      "$hard_cache, ".
									      "'$edit_path', ".
									      "'$man_path', ".
									      "'$del_path' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_vpage_templates_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$name = addslashes($this->get_name());
		$wp_path = addslashes($this->get_wp_path());
		$wp_class_name = addslashes($this->get_wp_class_name());
		$page_name = addslashes($this->get_page_name());
		$cache = $this->get_cache() ? 1 : 0;
		$cache_expiry = $this->get_cache_expiry();
		$hard_cache = $this->get_hard_cache() ? 1 : 0;
		$edit_path = addslashes($this->get_edit_path());
		$man_path = addslashes($this->get_man_path());
		$del_path = addslashes($this->get_del_path());

		$sql = "update tcms_vpage_templates set ".
						"name = '$name', ".
						"wp_path = '$wp_path', ".
						"wp_class_name = '$wp_class_name', ".
						"page_name = '$page_name', ".
						"cache = $cache, ".
						"cache_expiry = $cache_expiry, ".
						"hard_cache = $hard_cache, ".
						"edit_path = '$edit_path', ".
						"man_path = '$man_path', ".
						"del_path = '$del_path' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_vpage_templates where id = $id";
		$dbc->execute($sql);
	}


	public static function get_sql_for_grid() {
		$sql = "select id, name, wp_path, wp_class_name from tcms_vpage_templates";

		return $sql;
	}


	public static function get_sql_count_for_grid() {
		$sql = "select count(id) as total_rows from tcms_vpage_templates";

		return $sql;
	}


	public static function load_templates_ol_map(DB_Connection $dbc) {
		$sql = "select id, name from tcms_vpage_templates order by name asc";
		$rez = $dbc->execute($sql);

		$ret = array();
		$ids = array();

		while($rez_obj = $rez->fetch_object()) {
			array_push($ids, $rez_obj->ID);
			$ret['ol_map'][$rez_obj->ID] = $rez_obj->NAME;
		}

		if ($ids) {
			$ret['ids'] = $ids;
		}


		return $ret;
	}


	public static function load_templates_id_and_name(DB_Connection $dbc) {
		$sql = "select id, name from tcms_vpage_templates order by name asc";
		$rez = $dbc->execute($sql);

		$ret = array();
		$ids = array();

		$c = 0;
		while($rez_obj = $rez->fetch_object()) {
			$ret[$c]['id'] = $rez_obj->ID;
			$ret[$c]['name'] = $rez_obj->NAME;

			$c++;
		}


		return $ret;
	}




	public function load_by_wp_path(DB_Connection $dbc, $wp_path) {
		$wp_path = addslashes($wp_path);

		$ret = false;

		$sql = "select id from tcms_vpage_templates where wp_path = '$wp_path'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}


		return $ret;
	}
}